 import {localCache} from "@/utils/cache";
 import {MENULIST} from "@/global/constans";
 let menuList = localCache.getCache(MENULIST)
 // 将菜单转换为一维数组
function flattenMenuList(menus: any[]): any[] {
  let result: any[] = [];
  
  const flatten = (items: any[]) => {
    items?.forEach(item => {
      result.push(item);
      if (item.children && item.children.length > 0) {
        flatten(item.children);
      }
    });
  };
  
  flatten(menus);
  return result;
}

const flatMenuList = flattenMenuList(menuList);
let pathAry = flatMenuList.map(item => item.path);
 const asyncRoutes = [
  {
    path: '/system',
    name: 'system',
    component: import("@/views/main/index.vue"),
    meta: { title: '设置', icon: 'system' },
    hidden: true,
    children: [
      {
        path: '/system/permissionManage',
        name: 'permissionManage',
        hidden: true,
        meta: { title: '权限管理' },
        children: [
          {
            path: '/system/permissionManage/departmentManage',
            name: 'departmentManage',
            hidden: true,
            component: () => import('@/views/system/departmentManage.vue'),
            meta: { title: '部门管理', keepAlive: true }
          },
          {
            path: '/system/permissionManage/menuManage',
            name: 'menuManage',
            hidden: true,
            component: () => import('@/views/system/menuManage.vue'),
            meta: { title: '菜单管理', keepAlive: false }
          },
          {
            path: '/system/permissionManage/userManage',
            name: 'userManage',
            hidden: true,
            component: () => import('@/views/system/userManage.vue'),
            meta: { title: '用户管理', keepAlive: true }
          },
          {
            path: '/system/permissionManage/roleManage',
            name: 'roleManage',
            hidden: true,
            component: () => import('@/views/system/roleManage.vue'),
            meta: { title: '角色管理', keepAlive: true }
          },
          {
            path: '/system/permissionManage/postAuthority',
            name: 'postAuthority',
            hidden: true,
            component: () => import('@/views/system/postAuthority.vue'),
            meta: { title: '岗位管理', keepAlive: true }
          },
          {
            path: '/system/permissionManage/evaluationUser',
            name: 'evaluationUser',
            hidden: true,
            component: () => import('@/views/system/evaluationUser.vue'),
            meta: { title: '员工评价', keepAlive: true }
          }
        ]
      },
      {
        path: '/busManage',
        name: 'busManage',
        component: import("@/views/main/index.vue"),
        meta: { title: '班车管理', icon: 'busManage' },
        hidden: true,
        children: [
          {
            path: '/pathManage',
            name: 'pathManage',
            hidden: true,
            component: () => import('@/views/pathManage/index.vue'),
            meta: { title: '路线管理', keepAlive: true }
          },
          {
            path: '/busAppiont',
            name: 'busAppiont',
            hidden: true,
            component: () => import('@/views/busAppiont/index.vue'),
            meta: { title: '班车预约', keepAlive: true }
          }
        ]
      },
    ]
  },
]

let aryRoutes:any = []
function getRouters() {
  const handleRoutes = (routes:any) => {
    routes.forEach((route:any) => {
      aryRoutes.push(route)
      if (route.children && route.children.length > 0) {
        handleRoutes(route.children)
      }
    })
  }
  handleRoutes(asyncRoutes)
}
getRouters()
let resultAry:any = []
aryRoutes.forEach((route:any) => {
  if(pathAry.includes(route.name)){
    resultAry.push({name:route.name,path:'/'+route.name,component:route.component,meta:route.meta})
  }
})
export default resultAry